import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import WorkBenchView from '../views/WorkBenchView.vue'
import UserView from '../views/UserView.vue'
import WareHouseView from '../views/WareHouseView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      alias: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView,
      children: [
        {
          path: '/workbench',
          name: 'workbench',
          component: WorkBenchView,
        },
        {
          path: '/user',
          name: 'user',
          component: UserView,
        },
        {
          path: '/warehouse/',
          name: 'warehouse',
          component: WareHouseView,
        },

      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'notfound',
      component: () => import('../views/NotFoundView.vue'),
    }
  ]
})
// 导航守卫
router.beforeEach((to, from) => {
  let isAuth = sessionStorage.getItem('Authorization') ? true : false
  if (to.name != 'login' && to.name != 'notfound' && !isAuth)
    // 返回 false 以取消导航
    return { name: 'login' }

})

export default router
